package org.example.statemachine.controller;

import org.example.statemachine.entity.Order;
import org.example.statemachine.enums.OrderStatusChangeEvent;
import org.example.statemachine.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

/**
 * 订单接口，提供测试入口
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 创建订单
     */
    @PostMapping("/create")
    public Order createOrder(@RequestBody Order order) {
        // 生成唯一订单号
        order.setOrderCode(UUID.randomUUID().toString().replace("-", ""));
        return orderService.createOrder(order);
    }

    /**
     * 支付订单
     */
    @PostMapping("/{orderId}/pay")
    public boolean payOrder(@PathVariable Long orderId) {
        return orderService.changeOrderStatus(orderId, OrderStatusChangeEvent.PAYED);
    }

    /**
     * 发货
     */
    @PostMapping("/{orderId}/deliver")
    public boolean deliverOrder(@PathVariable Long orderId) {
        return orderService.changeOrderStatus(orderId, OrderStatusChangeEvent.DELIVERY);
    }

    /**
     * 确认收货
     */
    @PostMapping("/{orderId}/receive")
    public boolean receiveOrder(@PathVariable Long orderId) {
        return orderService.changeOrderStatus(orderId, OrderStatusChangeEvent.RECEIVED);
    }

    /**
     * 退货
     */
    @PostMapping("/{orderId}/reject")
    public boolean rejectOrder(@PathVariable Long orderId) {
        return orderService.changeOrderStatus(orderId, OrderStatusChangeEvent.REJECTED);
    }

    /**
     * 获取订单详情
     */
    @GetMapping("")
    public Order getOrder(@RequestParam Long orderId) {
        return new Order().setId(1L).setOrderCode("123").setName("张三").setStatus(1).setPrice(10.0);
    }
}